package io.babystar.modules.business.service.impl;

import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;
import io.babystar.modules.business.dao.PartnerTypeDao;
import io.babystar.modules.business.dao.ShopDao;
import io.babystar.modules.business.entity.PartnerTypeEntity;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.service.PartnerTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("partnerTypeService")
public class PartnerTypeServiceImpl extends ServiceImpl<PartnerTypeDao, PartnerTypeEntity> implements PartnerTypeService {
    @Autowired
    private ShopDao shopDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String shopId = (String) params.get("shopId");
        Long businessId = (Long) params.get("businessId");
        IPage<PartnerTypeEntity> page = this.page(
                new Query<PartnerTypeEntity>().getPage(params),
                new QueryWrapper<PartnerTypeEntity>()
                        .eq(businessId!=null,"business_id",businessId)
                        .eq(StringUtils.isNotBlank(shopId),"shop_id",shopId)
        );

        List<PartnerTypeEntity> list = page.getRecords();
        list.stream().forEach(x -> {
            ShopEntity shopEntity = shopDao.selectById(x.getShopId());
            if(shopEntity!=null){
                x.setShopName(shopEntity.getShopName());
            }
        });
        return new PageUtils(page);
    }

}